package com.delivery.service.impl;

import com.delivery.dto.SetmealDTO;
import com.delivery.dto.SetmealPageQueryDTO;
import com.delivery.entity.Setmeal;
import com.delivery.entity.SetmealDish;
import com.delivery.mapper.SetmealDishMapper;
import com.delivery.mapper.SetmealMapper;
import com.delivery.result.PageResult;
import com.delivery.service.SetmealService;
import com.delivery.vo.ComboContainedDishVO;
import com.delivery.vo.SetmealVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    SetmealMapper setmealMapper;

    @Autowired
    SetmealDishMapper setmealDishMapper;

    @Override
    public void addCombo(SetmealDTO setmealDTO) {

        Setmeal setmeal =new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }

    }

    /**
     * 根据套餐id查询套餐 会把套餐下的菜品也查询出来
     * @param id
     * @return
     */
    @Override
    public SetmealVO getComboById(Long id) {

        SetmealVO setmealVO = setmealMapper.selectById(id);
        return setmealVO;
    }

    /**
     * 分页查询套餐
     * 根据 分类ID，套餐名称，套餐状态 查询套餐
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.page(dto);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        // 修改 setmeal 表
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //  修改 setmeal_dish 表
        // 先删除 setmeal_dish 表中该套餐的记录
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());

        // 插入 setmeal_dish 表中该套餐的记录
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()){
            setmealDish.setSetmealId(setmealDTO.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    /**
     * 启售停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startStopCombo(Integer status, Long id) {
       Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
       setmealMapper.update(setmeal);

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void batchDeleteCombo(List<Long> ids) {
        // 需要额外删除 setmeal_dish 表中关于 这些套餐 的记录
        setmealDishMapper.deleteBatchBySetmealId(ids);

        // 删除 setmeal 表中关于 这些套餐 的记录
        setmealMapper.deleteBatchById(ids);

    }

    /**
     * 根据 分类ID 查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getCombosByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.selectCombosByCategoryId(categoryId);
        return setmeals;
    }

    /**
     * 根据 套餐ID 查询 套餐包含的菜品
     * @param setmealId 套餐ID
     * @return
     */
    @Override
    public List<ComboContainedDishVO> getContainedDishesByComboId(Long setmealId) {

        List<ComboContainedDishVO> comboContainedDishVOS = setmealDishMapper.selectContainedDishesByComboId(setmealId);
        return comboContainedDishVOS;
    }
}
